<?php

namespace App\Http\Controllers\Admin;

use Validator;
use App\Article;
use App\ArticleType;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    public function index(Request $request)
    {
        $articles = Article::orderBy('created_at', 'desc')->get();
        return view('admin.article.index', [
            'request' => $request,
            'articles' => $articles
        ]);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'type_id' => 'required',
                'title' => 'required',
                'content' => 'required'
            ];
            $messages = [
                'type_id.required' => '请选择栏目！',
                'title.required' => '请输入标题！',
                'content.required' => '请输入内容'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'code' => __LINE__,
                    'msg' => $validator->errors()->first()
                ];
                return response()->json($return);
            }
            $article = new Article();
            $article->type_id = $data['type_id'];
            $article->title = $data['title'];
            $article->content = $data['content'];
            $article->save();
            $return = ['code' => 0];
            return response()->json($return);
        }
        $articleTypes = ArticleType::where('type', '0')->get();
        return view('admin.article.add', [
            'request' => $request,
            'articleTypes' => $articleTypes
        ]);
    }

    public function edit(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'type_id' => 'required',
                'title' => 'required',
                'content' => 'required'
            ];
            $messages = [
                'type_id.required' => '请选择栏目！',
                'title.required' => '请输入标题！',
                'content.required' => '请输入内容'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'code' => __LINE__,
                    'msg' => $validator->errors()->first()
                ];
                return response()->json($return);
            }
            $article = Article::find($data['id']);
            $article->type_id = $data['type_id'];
            $article->title = $data['title'];
            $article->content = $data['content'];
            $article->save();
            $return = ['code' => 0];
            return response()->json($return);
        }
        $article = Article::find($id);
        $articleTypes = ArticleType::where('type', '0')->get();
        return view('admin.article.edit', [
            'request' => $request,
            'article' => $article,
            'articleTypes' => $articleTypes
        ]);
    }
}